﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
//using MenuItem = SMSController.MenuItem;



namespace SMSController
{
    public partial class frmSetting : Form
    {
        private static string languageString = @"中文";
        public frmSetting()
        {
            InitializeComponent();
            noti.Click += new EventHandler(noti_Click);
        }

        void noti_Click(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            this.Visible = true;
        }


       private void frmSetting_Load(object sender, EventArgs e)
        {
            initForm();
        }

        private void initForm()
        {
            //read config.xml
            LangPack lp = new LangPack(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config.xml",languageString,this);
            //MessageBox.Show(lp.getFormText());
            //this.Text = lp.getFormText();

            
            
            
            
            // language
            changeLanguage(languageString);
            
            //init language menu
            menuItemLanguage.MenuItems.Clear();
            string[] menuText = lp.getLanguageTexts();
            for (int i = 0; i < menuText.Length; i++)
            {
                MenuItem menuLang = new MenuItem();
                menuLang.Click += new EventHandler(menuLang_Click);
                menuLang.Text = menuText[i];
                menuItemLanguage.MenuItems.Add(menuLang);
            }



            Rules rules = new Rules(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config.xml");
            txtHead.Text = rules.getHead().Trim();
            if (rules.isAll())
            {
                chkALL.Checked = true;
                listAcceptable.Enabled = false;
                btnAAdd.Enabled = false;
                btnADel.Enabled = false;
            }
            else
            {
                chkALL.Checked = false;
                listAcceptable.Enabled = true;
                btnAAdd.Enabled = true;
                btnADel.Enabled = true;
            }
            string[] tmpAcceptableNumber = Regex.Split(rules.getAllAcceptableNumber("##@@##"), "##@@##");
            listAcceptable.Items.Clear();
            for (int i = 0; i < tmpAcceptableNumber.Length; i++)
            {
                listAcceptable.Items.Add(tmpAcceptableNumber[i]);
            }


            string[] tmpShortcutTitle = Regex.Split(rules.getAllShortcutTitle("##@@##"), "##@@##");
            listShortcuts.Items.Clear();
            for (int i = 0; i < tmpShortcutTitle.Length; i++)
            {
                listShortcuts.Items.Add(tmpShortcutTitle[i]);
            }
            //rules = null;
        }

        void menuLang_Click(object sender, EventArgs e)
        {
            MenuItem mu =(MenuItem) sender;
            changeLanguage(mu.Text); 
        }
        
        void changeLanguage(string language)
        {
            LangPack lp = new LangPack(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config.xml",language, this);
            this.Text = lp.getControlText(this.Name);
            foreach (Control ct in this.Controls)
            {
                try
                {
                    ct.Text = lp.getControlText(ct.Name);
                }
                catch
                {

                }
            }
            languageString = language;
        
            //for (int i=0;i<mm.MenuItems.Count;i++)
            //{
            //    try
            //    {
            //        mm.MenuItems[i].Text = lp.getControlText(mm.MenuItems[i].Text);
            //        //mi.Text = lp.getControlText(mi.Tag.ToString());
            //    }
            //    catch
            //    {

            //    }
            //}
        }



        private void menuItemMini_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        private void btnADel_Click(object sender, EventArgs e)
        {
            if (listAcceptable.SelectedItem != null)
            {
                Rules rules = new Rules(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config.xml");
                rules.delAcceptableNumber(listAcceptable.SelectedItem.ToString().Trim());
                initForm();
            }
        }

        private void chkALL_Click(object sender, EventArgs e)
        {
            Rules rules = new Rules(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config.xml");

            if (chkALL.Checked)
            {
                rules.setIsAll(true);
            }
            else
            {
                rules.setIsAll(false);
            }
            initForm();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (listShortcuts.SelectedItem != null)
            {
                Rules rules = new Rules(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config.xml");
                rules.delShortcut(listShortcuts.SelectedItem.ToString().Trim());
                initForm();
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (listShortcuts.SelectedItem != null)
            {
                frmSub frmModifyShortcuts = new frmSub(1 , listShortcuts.SelectedItem.ToString().Trim(),languageString);
                frmModifyShortcuts.Text = @"Modify Shortcut";
                frmModifyShortcuts.Show();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmSub frmModifyShortcuts = new frmSub(3, "", languageString);
            frmModifyShortcuts.Text = @"Add New Shortcut";
            frmModifyShortcuts.Show();
        }


        private void btnAAdd_Click(object sender, EventArgs e)
        {
            frmSub frmModifyShortcuts = new frmSub(2, "", languageString);
            frmModifyShortcuts.Text = @"Add New Acceptable Number";
            frmModifyShortcuts.Show();
        }

        private void frmSetting_GotFocus(object sender, EventArgs e)
        {
            initForm();
        }

        private void btnHUpdate_Click(object sender, EventArgs e)
        {
            Rules rules = new Rules(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config.xml");
            rules.setHead(txtHead.Text.Trim());
            initForm();
        }

        private void menuItemQuit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void txtHead_TextChanged(object sender, EventArgs e)
        {
            if (txtHead.Text.Trim().Length == 0)
                btnHUpdate.Enabled = false;
            else
                btnHUpdate.Enabled = true;
        }





    }



}